package com.minivision.sdktestdemo.ui.view;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.event.DataModificationEvent;
import com.minivision.sdktestdemo.model.AlbumProvider;
import com.minivision.sdktestdemo.model.MusicProvider;
import com.minivision.sdktestdemo.model.SingerProvider;
import com.minivision.sdktestdemo.utils.AlertDialogUtils;
import com.minivision.sdktestdemo.utils.TextFormatUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by devtang on 2016/5/17.
 */
public class MusicInfoContentView implements PopContentView, PlayBeanContentView, PopClickItemFactory.OnMusicChangedListener {

    private Activity mContext;

    private View mContentView;

    public MusicInfoContentView(Activity context) {
        mContext = context;
        initView();
        mExecutor = Executors.newSingleThreadExecutor();
    }

    private String[] mTitles = {"文件名称", "播放时长", "文件音质", "文件大小", "文件路径"};

    private void initView() {
        for (int i = 0; i < mTitles.length; i++) {
            setTitle(i + 1, mTitles[i]);
        }
        Button mBtn = (Button) getContentView().findViewById(R.id.btn_change);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicInfoContentView.this.dismiss();
                AlertDialog dialog = AlertDialogUtils.getChangedDialog(getContext(), MusicInfoContentView.this);
                dialog.show();
                mMusicName = mBean.getmMusicName();
                mSingerName = mBean.getmSingerName();
                mAlbumName = mBean.getmAlbumName();
            }
        });
    }

    private MusicBean mBean;

    private String mMusicName;

    private String mSingerName;

    private String mAlbumName;

    public void setTitle(int pos, String text) {
        String title = "tv_title";
        TextView textView = (TextView) getContentView().findViewById(mContext.getResources().getIdentifier(title + pos, "id", mContext.getPackageName()));
        textView.setText(text);
    }

    public void setContent(int pos, String text) {
        String content = "tv_content";
        TextView textView = (TextView) getContentView().findViewById(mContext.getResources().getIdentifier(content + pos, "id", mContext.getPackageName()));
        textView.setText(text);
    }

    @Override
    public int getMaxPopWindowHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    @Override
    public View getContentView() {
        if (mContentView == null) {
            mContentView = mContext.getLayoutInflater().inflate(R.layout.pop_music_info_view, null);
        }
        return mContentView;
    }

    @Override
    public void dismiss() {
        if (mPopWindow != null) {
            mPopWindow.dismiss();
        }
    }

    private PopupWindow mPopWindow;

    private ExecutorService mExecutor;

    @Override
    public void setPopupWindow(PopupWindow popupWindow) {
        mPopWindow = popupWindow;
    }

    @Override
    public Activity getContext() {
        return mContext;
    }

    public void setPlayBean(MusicBean bean) {
        mBean = bean;
        setTitle(0, "歌曲:" + bean.getmMusicName());
        setContent(1, bean.getmPath().substring(bean.getmPath().lastIndexOf(File.separator) + 1));
        setContent(2, TextFormatUtils.getMusicTime(bean.getmLen()));
        setContent(3, "");
        setContent(4, String.format("%.1fM", (double) bean.getmSize() / (1024.0 * 1024.0)));
        setContent(5, bean.getmPath().substring(0, bean.getmPath().lastIndexOf(File.separator)));
    }

    @Override
    public void onChanged() {
        showProgressBack(true);
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                boolean flag = false;
                if (!mMusicName.equals(mBean.getmMusicName())) {
                    if (mBean.getmMusicName().isEmpty()) {
                        mBean.setmMusicName("未知");
                    }
                    flag = true;
                }
                if (!mSingerName.equals(mBean.getmSingerName())) {
                    if (mBean.getmSingerName().isEmpty()) {
                        mBean.setmSingerName("未知");
                    }
                    flag = true;
                    SingerProvider.getInstance(MusicProvider.getInstance(mContext)).update(mBean, mBean.getmSingerName());
                }
                if (!mAlbumName.equals(mBean.getmAlbumName())) {
                    if (mBean.getmAlbumName().isEmpty()) {
                        mBean.setmAlbumName("未知");
                    }
                    flag = true;
                    AlbumProvider.getInstance(MusicProvider.getInstance(mContext)).update(mBean, mBean.getmAlbumName());
                }
                if (flag) {
                    DataModificationEvent event = new DataModificationEvent(new ArrayList<MusicBean>(), DataModificationEvent.ACTION_CHANGE);
                    event.mBeans.add(mBean);
                    MusicProvider.getInstance(mContext).update(mBean);
                    MusicProvider.getInstance(mContext).sort(MusicProvider.getInstance(mContext).getList());
                    EventBus.getDefault().post(event);
                }
                mContext.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showProgressBack(false);
                    }
                });
            }
        });
    }

    @Override
    public MusicBean getBean() {
        return mBean;
    }

    private PopupWindow mProgressWindow;

    public void showProgressBack(boolean flag) {
        if (mProgressWindow == null) {
            mProgressWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mProgressWindow.setContentView(getContext().getLayoutInflater().inflate(R.layout.layout_update, null));
        }
        if (flag) {
            mProgressWindow.showAtLocation(getContext().findViewById(R.id.layout_main), Gravity.CENTER, 0, 0);
        } else {
            mProgressWindow.dismiss();
        }
    }

    @Override
    public void setPlayBean(List<MusicBean> list, MusicBean bean) {
        setPlayBean(bean);
    }
}
